package com.lechi.web.runner;

import com.lechi.web.common.utils.DateUtils;
import com.lechi.web.job.GetSensorDataJob;
import com.lechi.web.quartz.scheduler.DynamicJob;
import com.lechi.web.quartz.scheduler.DynamicSchedulerFactory;
import com.lechi.web.service.ISchedulerService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 项目启动后添加定时轮训任务
 */
@Slf4j
@Order(7)
@Component
public class StartFtpSchedule implements CommandLineRunner {

    private static final Map<String, String> map = new HashMap<>();

    static {
        map.put("1", "1min");
        map.put("2", "10min");
        map.put("3", "60min");
        map.put("4", "24hour");

        map.put("1_cron", "60");
        map.put("2_cron", "600");
        map.put("3_cron", "3600");
        map.put("4_cron", "72000");
    }

    @Autowired
    private ISchedulerService iSchedulerService;

    @Autowired
    private DynamicSchedulerFactory dynamicSchedulerFactory;


    /**
     * Get date.
     *
     * @param type the type
     * @return the date
     */
    public static Date get(int type) {
        Date beginTime = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(beginTime);
        if (type == 1) {
            int mim = calendar.get(Calendar.MINUTE);
            calendar.set(Calendar.MINUTE, mim + 1);
            calendar.set(Calendar.SECOND, 3);
        }
        if (type == 2) {
            int min = calendar.get(Calendar.MINUTE);
            min = min > 10 ? (min / 10 + 2) * 10 : 20;
            calendar.set(Calendar.MINUTE, min);
            calendar.set(Calendar.SECOND, 1);
        }
        if (type == 3) {
            calendar.add(Calendar.HOUR, 1);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 2);
        }
        if (type == 4) {
            calendar.set(Calendar.HOUR_OF_DAY, 12);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 3);
        }
        return calendar.getTime();
    }

    public void run(String... args) throws SchedulerException {
        for (int i = 1; i < 5; i++) {
            String jobName = "SensorDataJob_" + map.get(String.valueOf(i));
            iSchedulerService.removeJob(jobName);
            DynamicJob dynamicJob = new DynamicJob(jobName)
                    .cronExpression(map.get(i + "_cron"))
                    .target(GetSensorDataJob.class);
            dynamicJob.addJobData("cron", map.get(String.valueOf(i)));

            final Date oneMinBeginTime = get(i);
            dynamicSchedulerFactory.registerJob(dynamicJob, oneMinBeginTime);
            log.error("注册调度任务BatchInsertJob [{}] -> {}", dynamicJob, DateUtils.dateTime(oneMinBeginTime));
        }
    }
}


